﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ECSE6770.Models;

namespace ECSE6770.Controllers
{
    [RequireHttps]
    [Authorize(Roles = "Trainer,Admin")]
    public class TrainerController : BaseController
    {
        public ActionResult Index()
        {
            return View(ContextManager.CurrentUser.Interop);
        }

        public ActionResult ModuleDetails(int id)
        {
            return View(Db.Modules.First(m => m.Id == id));
        }

        public ActionResult EmailUsers(int id)
        {
            ViewBag.EnableAdminScripts = true; //we'll need the wysiwyg editor
            ViewBag.Module = Db.Modules.First(m => m.Id == id);
            return View();
        }

        [HttpPost]
        public ActionResult EmailUsers(EmailModel model)
        {
            if (ModelState.IsValid)
            {
                int id = Convert.ToInt32(this.RouteData.Values["id"]);

                Module module = Db.Modules.First(m => m.Id == id);
                BccEmailBlast(ContextManager.CurrentUser.EmailAddress,
                    model.Subject,
                    model.Body,
                    module.RegisteredUsers().Select(u => u.EmailAddress));

                return Success("Email Sent", String.Format("Your email has been sent to the registered users of {0}.", module.Title));
            }
            else
                return View();
        }
    }
}
